/*************************************************************************
 *  Copyright © 2023-2030 FXB CO.,LTD. All rights reserved.
 *------------------------------------------------------------------------
 *  公司：DefaultCompany
 *  项目：UPMProject
 *  文件：PackageList.cs
 *  作者：Administrator
 *  日期：2025/2/11 16:31:0
 *  功能：Nothing
*************************************************************************/

using System.Collections;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

namespace SVNPackageManager.Runtime
{
    [System.Serializable]
    public class PackageAddress
    {
        public string registryName;
        public List<string> packageUrls = new List<string>();
    }

    [CreateAssetMenu(fileName = "PackageListCache", menuName = "ScriptableObjects/PackageListCache", order = 1)]
    public class PackageListCache : ScriptableObject
    {
        [HideInInspector]
        public List<PackageItemInfo> packageConfigs = new List<PackageItemInfo>();

        [HideInInspector]
        public List<PackageAddress> packageUrls = new List<PackageAddress>();

        public bool IsExist(string key, out PackageItemInfo Info)
        {
            PackageItemInfo itemInfo = packageConfigs.FirstOrDefault(item => item.packageSvnUrl == key);
            Info = itemInfo;
            return itemInfo != null;
        }

        public void UpdatePackageState(string packageItemID, bool state)
        {
            List<PackageItemInfo> packageItem = packageConfigs.Where(item => item.packageItemID == packageItemID).ToList();
            for (int i = 0; i < packageItem.Count; i++) 
            {
                if (packageItem[i] != null)
                {
                    packageItem[i].isInstalled = state;
#if UNITY_EDITOR
                    EditorUtility.SetDirty(this);
                    AssetDatabase.SaveAssets();
#endif
                }
            }
        }

        public void AddPackageItemToCache(PackageItemInfo packageInfo)
        {
            packageConfigs.Add(packageInfo);
        }

        public void AddPackageUrlsToCache(string registryName, List<string> packageUrl)
        {
            PackageAddress address = GetPackageUrlsObjFromCache(registryName);
            if (address != null)
            {
                address.packageUrls = packageUrl;
            }
            else
            {
                PackageAddress groupAddress = new PackageAddress()
                {
                    registryName = registryName,
                    packageUrls = packageUrl
                };

                packageUrls.Add(groupAddress);
            }
        }

        public List<string> GetPackageUrlsFromCache(string registryName)
        {
            PackageAddress address = GetPackageUrlsObjFromCache(registryName);
            if (address != null)
            {
                return address.packageUrls;
            }
            return null;
        }

        public PackageAddress GetPackageUrlsObjFromCache(string registryName)
        {
            return packageUrls.FirstOrDefault(item => item.registryName == registryName);
        }


        public void SavePackageListCache()
        {
#if UNITY_EDITOR
            EditorUtility.SetDirty(this);
            AssetDatabase.SaveAssets();
#endif
        }
    }
}


